
package com.kugou.game.sdk.protocol;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.text.TextUtils;

import com.kugou.framework.base.AppException;
import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.framework.utils.UrlEncodeUtil;
import com.kugou.game.sdk.entity.KubiPayRequestParams;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;
import com.kugou.game.sdk.utils.RSAUtil;

/**
 * 游币支付请求类
 * 
 * @author ljc
 * @since 2013-12-5 下午7:59:41
 */
public class KubiPayTask {

    /**
     * 游币支付请求接口
     * 
     * @param payParams
     * @return
     */
    public KubiPayHttpResponse request(KubiPayRequestParams payParams) {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();

        // 填充参数，key-value。key是接口要求传的变量名称
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", ProtocolCmd.CMD_RECHARGE_BY_YOUBI);
        params.put("username", payParams.getUserName());
        params.put("outorderid", payParams.getOutOrderId());
        params.put("kubinum", payParams.getMoney());
        params.put("role", payParams.getRole());
        params.put("kgtoken", payParams.getKugouToken());
        params.put("paypwd",
                RSAUtil.getRASEncrypt(UrlEncodeUtil.encode(payParams.getPayPasswd(), "utf-8")));
        if (!TextUtils.isEmpty(payParams.getExtension1())) {
            params.put("extension1", payParams.getExtension1());
        }
        if (!TextUtils.isEmpty(payParams.getExtension2())) {
            params.put("extension2", payParams.getExtension2());
        }

        params.put("couponid", payParams.getCouponId());

        requestParams.add(params);

        // 生成请求包
        HttpBaseRequestPackage requestPackage = new HttpBaseRequestPackage();
        requestPackage.setUserName(payParams.getUserName());
        requestPackage.setKgToken(payParams.getKugouToken());
        // 生成响应包
        ResponsePackage responsePackage = new HttpResponsePackage();
        // 生成响应类
        KubiPayHttpResponse appsHttpResponse = new KubiPayHttpResponse();

        requestPackage.setRequestParams(requestParams);

        try {
            KGHttpClient.request(requestPackage, responsePackage);
            // 响应类赋值
            responsePackage.getResponseData(appsHttpResponse);
            // 返回响应类
            return appsHttpResponse;
        } catch (AppException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 响应包
    private class HttpResponsePackage extends HttpBaseResponsePackage<KubiPayHttpResponse> {

        @Override
        protected void handleResponse(KubiPayHttpResponse response, String jsonStr) {
            try {
                LogUtil.d("response", "KubiPayTask:" + jsonStr);
                JSONArray responseArray = new JSONArray(jsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    JSONObject jsonResp = responseArray.getJSONObject(i);
                    String code = jsonResp.optString("code");
                    String message = jsonResp.optString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);
                    if (response.isUseful()) {
                        JSONObject jsonResult = jsonResp.getJSONObject("data");
                        response.setResult_code(jsonResult.getString("code"));
                        response.setResult_message(jsonResult.getString("message"));
                        if (!jsonResult.isNull("kbleft")) {
                            response.setLeftKubi(jsonResult.getString("kbleft"));
                        }
                    }
                }
                response.setOk(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 响应类，这个类主要用于存储返回数据中的各个字段。
     * 
     * @author ljc
     * @since 2013-12-5 上午9:33:38
     */
    public class KubiPayHttpResponse extends ResponseMessage {

        /**
         * 
         */
        private static final long serialVersionUID = -1982675518433005547L;

        private String result_code;

        private String result_message;

        private String leftKubi = "";

        public String getResult_code() {
            return result_code;
        }

        public void setResult_code(String result_code) {
            this.result_code = result_code;
        }

        public String getResult_message() {
            return result_message;
        }

        public void setResult_message(String result_message) {
            this.result_message = result_message;
        }

        public String getLeftKubi() {
            return leftKubi;
        }

        public void setLeftKubi(String leftKubi) {
            this.leftKubi = leftKubi;
        }
    }
}
